// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// All of the Node.js APIs are available in this process.
// 引入ipcRenderer
const electron = require('electron')
const {
    shell,
    ipcRenderer
} = electron
let Timer
let lastMessage

function notice(scope, data) {
    // {current_page: 1, notis: Array(7), total_count: 7, total_pages: 1}
    // current_page: 1
    // notis: (7) [{…}, {…}, {…}, {…}, {…}, {…}, {…}]
    // total_count: 7
    // total_pages: 1
    // __proto__: Object
    if (data.notis) {   
        for (const msg of data.notis.reverse()) {
            const message = {
                title: `You have ${data.total_count} new ${scope}`,
                body: `-\n${msg.message ? msg.message : msg.content} ${msg.namespace ? "from " + msg.namespace.name : ""}`
            }
            const notifications = new Notification(message.title,message)
            notifications.click = () => {
                console.log('Notification clicked')
                shell.openExternal('https://gitee.com/notifications')
            }
        }
    }
}

function dispatch(unread) {
    //{infos: 5, messages: 0, referer: 0, total: 5}
    if (lastMessage !== unread.total) {
        console.log("last message:")
        console.log(lastMessage)
        ipcRenderer.send('refresh-window')
        lastMessage = unread.total
    }
    let args = {}
    switch (true) {
        case unread.infos > 0:
            args = {
                url: "/notifications/all",
                data: {
                    scope: 'infos',
                }
            }
            const infos = ipcRenderer.sendSync('ajax-get', args)
            notice("infos",infos)
            break
        case unread.messages > 0:
            args = {
                url: "/notifications/all",
                data: {
                    scope: 'messages',
                }
            }
            const messages = ipcRenderer.sendSync('ajax-get', args)
            notice("messages",messages)
            break
        case unread.referer > 0:
            args = {
                url: "/notifications/all",
                data: {
                    scope: 'referer',
                }
            }
            const referer = ipcRenderer.sendSync('ajax-get', args)
            notice("messages @ you",referer)
            break
        default:
            return false
    }
    return true
}

function unread_count() {
    const args = {
        url: "/notifications/unread_count"
    }
    const res = ipcRenderer.sendSync('ajax-get', args)
    return res ? dispatch(res) : false
}
$(document).ready(function () {
    console.log("ready!");
    Timer = setInterval(unread_count, 60*1000)
});


// Below is the test code
document.getElementById("test").addEventListener("click", unread_count);

document.getElementById("message").addEventListener("click", function () {
    const args = {
        url: "/notifications/all",
        data: {
            scope: 'messages',
            page: 0
        }
    }
    const info = ipcRenderer.sendSync('ajax-get', args)
    console.log(info)
});

document.getElementById("infos").addEventListener("click", function () {
    const args = {
        url: "/notifications/all",
        data: {
            scope: 'infos',
            page: 0
        }
    }
    const info = ipcRenderer.sendSync('ajax-get', args)
    console.log(info)
});

document.getElementById("referer").addEventListener("click", function () {
    const args = {
        url: "/notifications/all",
        data: {
            scope: 'referer',
            page: 0
        }
    }
    const info = ipcRenderer.sendSync('ajax-get', args)
    console.log(info)
});